<template>
  <Head />
  <div class="banner">
    <div class="content">
      <h1>Asset</h1>
    </div>
    <div class="card" @click="onSubmit">
      <div class="title">
        {{ $t("withdrawCoins") }}
      </div>
      <div class="right">
        <span>{{ assets?.coin }}</span>
        <span>{{ assets?.balance }}</span>
      </div>
    </div>
    <div class="wrapper">
      <div class="text">{{ $t("coinEarningServiceGas") }}3%，1000BT2{{ $t("raise") }}</div>
    </div>
  </div>
  <div class="body">
    <div class="item" @click="getRecord(item.type)" v-for="(item, index) in cashList" :key="index">
      <img src="@/assets/image/eicIconMedium.svg" />
      <div class="asset">{{ item.name }}</div>
      <div class="right">
        <span>{{ item.cash }}</span><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-1h14dtc" focusable="false"
          aria-hidden="true" viewBox="0 0 24 24" data-testid="ArrowForwardIosIcon">
          <path d="M6.23 20.23 8 22l10-10L8 2 6.23 3.77 14.46 12z"></path>
        </svg>
      </div>
    </div>
  </div>

  <van-popup v-model:show="showBottom" position="bottom" title="提现" class="WithdrawPopup" :closeable="true" :style="{
    padding: '18px 20px 40px',
    background: '#1a1d1a',
    borderRadius: '10px 10px 0 0',
  }">
    <div class="WithdrawContent">
      <div class="title">{{ $t('operate') }}</div>
      <van-button type="primary" size="normal" class="btn btn1"
        @click="onWithdraw">{{ $t('confirmWithdrawalBtn') }}</van-button>
      <van-button type="success" size="normal" class="btn btn2"
        @click="getWithdrawRecord">{{ $t('withdrawalsRecord') }}</van-button>
    </div>
  </van-popup>
</template>

<script setup lang="ts">
import { Async_userMyWallet, Async_cashAll, Async_inoutWithdraw } from "@/server/api";
import { showToast, showDialog } from "vant";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
const router = useRouter();
let loading = ref(false)
let assets = ref<IAssets>()
const cashList = ref([
  { name: t("incomeReward"), cash: 0, type: 'EARNINGS', },
  { name: t("giftNewlyweds"), cash: 0, type: 'NEWUSER' },
  { name: t("sinkingInvitationReward"), cash: 0, type: 'INVITE' },
  { name: t("contributionReward"), cash: 0, type: 'CONTRIB' },
  { name: t("nodeReward"), cash: 0, type: 'NODE' },
])
interface IAssets {
  code: string
  address: string
  coin: string
  frozenMoney: Number
  balance: Number
  exchangeRate: Number
  exchangeSymbol: string
  exchangeAmount: Number
  createAt: Number
  logo: string
  updateAt: Number
}
let showBottom = ref(false)
const getList = async () => {
  try {
    let res: IAssets = await Async_userMyWallet();
    assets.value = res
  } catch (error) {
    console.error(error);
  }
};

const getCashAll = async () => {
  try {
    let res: any = await Async_cashAll();
    if (res) {
      cashList.value[0].cash = res.earnings;
      cashList.value[1].cash = res.newUser;
      cashList.value[2].cash = res.invite;
      cashList.value[3].cash = res.contrib;
      cashList.value[4].cash = res.node;
    }
  } catch (error) {
    console.error(error);
  }
};

const getRecord = async (type) => {
  router.push({
    path: 'assetsRecord',
    query: {
      type
    }
  });
}

const getWithdrawRecord = async () => {
  router.push({
    path: 'withdrawRecord',
  });
}

// 提现
const onSubmit = () => {
  showBottom.value = true
};

const onWithdraw = () => {
  if (Number(assets.value.balance) < 1000) {
    return showToast(t("cannotWithdrawMoneyBelow1000"));
  }
  showDialog({
    message: t('confirmWithdrawal'),
  }).then(async () => {
    try {
      loading.value = true;
      let res = await Async_inoutWithdraw({
        type: assets.value.coin,
      })
      if (res) {
        showToast(res);
        showBottom.value = false
      }
      getList();
      getCashAll();
    } catch (error) {
      console.log(error)
    } finally {
      loading.value = false;
    }
  })
    .catch(() => {
      // on cancel
    });
}


onMounted(() => {
  getList();
  getCashAll();
});


</script>
<style scoped lang="scss">
$theme: #59e457;

.banner {
  background-image: url("@/assets/image/assetBg.png");
  background-position-x: center;
  background-position-y: 40px;
  background-repeat: no-repeat;
  background-size: 240px 240px;

  h1 {
    color: $theme;
    font-family: Ethnocentric;
    font-size: 20px;
    font-weight: 400;
    line-height: 20px;
    margin: 0 0 4px;
    text-align: right;
    color: transparent;
  }

  .content {
    margin-inline: 30px;
    margin-bottom: 159px;
    padding-top: 76px;
    position: relative;

    &::after {
      background: radial-gradient(50% 50% at 50% 50%,
          #59e4574d 0,
          #59e45700 100%);
      content: "";
      display: block;
      height: 206px;
      position: absolute;
      top: -4%;
      width: 100%;
    }
  }

  .card {
    align-items: flex-end;

    border-radius: 5px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 85px;
    margin-inline: 30px;
    padding: 20px;
    position: relative;
    background: $theme;
    z-index: 1;

    &::after {
      content: "";
      position: absolute;
      top: 50%;
      left: 50%;
      width: 50%;
      height: 100%;
      background: url("@/assets/image/logo.png");
      background-position: top center;
      background-size: 46% 100%;
      background-repeat: no-repeat;
      z-index: -1;
      transform: translateX(-50%) translateY(-50%) skewY(10deg) skewX(10deg) scale(0.8);
      // filter: blur(1px);
      // opacity: 0.4;
    }

    .title {
      color: #000;
      // font-family: Ethnocentric;
      font-size: 24px;
      line-height: 20px;
      margin: 0 0 4px;
      font-weight: bold;
      text-align: right;
    }

    .right {
      display: flex;
      flex-direction: column;
      text-align: right;

      span {
        color: #0c0c0c;

        &:last-child {
          color: #000;
          font-family: Inter;
          font-size: 30px;
          font-weight: 700;
        }
      }
    }
  }

  .wrapper {
    gap: 13px;
    margin-bottom: 20px;
    margin-top: 13px;
    margin-inline: 30px;
    background-color: #1a1d1a;
    padding: 12px 24px;
    border: none;
    border-radius: 5px;

    .text {
      display: block;
      color: #ccc;
      font-size: 16px;
      font-weight: 700;
      margin: 0;
    }
  }
}

.body {
  background-color: #1a1d1a;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  min-height: 46.05vh;
  padding: 16px;

  img {
    width: 35px;
    height: 35p;
  }

  .item {
    align-items: center;
    background-color: #444444;
    border-radius: 5px;
    cursor: pointer;
    display: flex;
    gap: 14px;
    justify-content: space-between;
    margin-bottom: 12px;
    overflow: hidden;
    padding: 12px 16px 12px 12px;
  }

  .asset {
    color: #888888;
    font-size: 18px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .right {
    align-items: center;
    display: flex;
    flex: 1 1;
    gap: 14px;
    justify-content: flex-end;
    overflow: hidden;

    span {
      color: #fff;
      font-size: 18px;
      font-weight: 700;
      overflow: hidden;
      text-overflow: ellipsis;
      vertical-align: text-top;
      white-space: nowrap;
    }

    svg {
      fill: #fff;
      height: 10px;
      width: 10px;
    }
  }
}

.WithdrawContent {
  display: grid;
  grid-template-columns: repeat(1, 1fr);
  gap: 10px;

  .title {
    text-align: center;
    color: #fff;
    margin-bottom: 20px;
  }

  .btn {
    height: 40px;
    align-items: center;
    background: #1a1d1a;
    border-radius: 11px;
    display: inline-flex;
    gap: 10px;
    font-size: 12px;
    justify-content: center;
    padding: 12px 20px;

    &.btn1 {
      background: rgba(89, 228, 87, .5);
      color: #fff;
    }

    &.btn2 {
      border-color: rgba(89, 228, 87, .5);
      color: #fff;
    }
  }
}
</style>
